<?php

namespace App\Http\Middleware;

use App\Models\Official;
use Closure;
use Illuminate\Support\Facades\Route;
use EasyWeChat\Factory;
use App\Models\User;
use Illuminate\Support\Facades\Log;

class MemberSave
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $user = session('wechat.oauth_user.default'); // 拿到授权用户资料
        //$user->id   公众号的openid
        $official = Official::where('openid', $user->id)->first();
        if (empty($official)) {
            //未登录，跳转
            return redirect()->route('wechat.login');
        }

        return $next($request);
    }

}
